package net.htjs.mobile.android.crm.util;

import java.io.DataOutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

import net.htjs.mobile.android.crm.R;


import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Environment;

public class Util {

	/**
	 * 判断sdcard是否存在；
	 * @return
	 */
	public static boolean isSdCardExsit(){
		return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
	}
	
	public static boolean RootCmd(String cmd) {
		Process process = null;
		DataOutputStream os = null;
		try {
			process = Runtime.getRuntime().exec("su");
			os = new DataOutputStream(process.getOutputStream());
			os.writeBytes(cmd + "\n");
			os.writeBytes("exit\n");
			os.flush();
			process.waitFor();
		} catch (Exception e) {
			return false;
		} finally {
			try {
				if (os != null) {
					os.close();
				}
				System.out.println(cmd);
				process.destroy();
			} catch (Exception e) {
			}
		}
		return true;
	}

	public static Object getProperty(Object owner, String fieldName)
			throws Exception {
		Class ownerClass = owner.getClass();

		Field field = ownerClass.getField(fieldName);

		Object property = field.get(owner);

		return property;
	}

	// 得到对象的静态属性
	public Object getStaticProperty(String className, String fieldName)
			throws Exception {
		Class ownerClass = Class.forName(className);

		Field field = ownerClass.getField(fieldName);

		Object property = field.get(ownerClass);

		return property;
	}

	public static Object invokeMethod(Object owner, String methodName,
			Object[] args) throws Exception {

		Class ownerClass = owner.getClass();

		if (args == null) {
			Method method = ownerClass.getMethod(methodName, null);
			return method.invoke(owner, null);
		}

		Class[] argsClass = new Class[args.length];

		for (int i = 0, j = args.length; i < j; i++) {
			argsClass[i] = args[i].getClass();
		}
		Method method = ownerClass.getMethod(methodName, argsClass);
		return method.invoke(owner, args);
	}

	// 执行类静态方法
	public Object invokeStaticMethod(String className, String methodName,
			Object[] args) throws Exception {
		if (className == null || methodName == null) {
			return null;
		}
		Class ownerClass = Class.forName(className);
		Class[] argsClass = new Class[args.length];

		for (int i = 0, j = args.length; i < j; i++) {
			argsClass[i] = args[i].getClass();
		}

		Method method = ownerClass.getMethod(methodName, argsClass);

		return method.invoke(null, args);
	}

	public static String dialogOnlyCommit(Context c, String msg, String post,
			String nega, final Object owner, final String methodName,
			final Object[] args) {
		AlertDialog.Builder builder = new Builder(c);
		builder.setMessage(msg);
		builder.setTitle("提示");
		builder.setIcon(R.drawable.logo);
		builder.setPositiveButton(post, new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				dialog.dismiss();
				try {
					invokeMethod(owner, methodName, args);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		builder.create().show();
		return L.SUCCESS;
	}

	public static String dialog(Context c, String msg, String post,
			String nega, final Object owner, final String methodName,
			final Object[] args) {
		AlertDialog.Builder builder = new Builder(c);
		builder.setMessage(msg);
		builder.setTitle("提示");
		builder.setIcon(R.drawable.logo);
		builder.setPositiveButton(post, new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				dialog.dismiss();
				try {
					invokeMethod(owner, methodName, args);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});

		builder.setNegativeButton(nega, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		builder.create().show();
		return L.SUCCESS;
	}

	public static String dialog(Context c, String msg, String post,
			String nega, final Object owner, final String methodName,
			final Object[] args, final Object owner2, final String methodName2,
			final Object[] args2, final boolean is2after1) {
		AlertDialog.Builder builder = new Builder(c);
		builder.setMessage(msg);
		builder.setTitle("提示");

		builder.setPositiveButton(post, new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				dialog.dismiss();
				try {
					invokeMethod(owner, methodName, args);
					if (is2after1) {
						invokeMethod(owner2, methodName2, args2);
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});

		builder.setNegativeButton(nega, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				try {
					invokeMethod(owner2, methodName2, args2);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		builder.create().show();
		return L.SUCCESS;
	}

	public static void dialogProcess(Context c, String msg, String post,
			String nega, final Object owner, final String methodName,
			final Object[] args, final Object owner2, final String methodName2,
			final Object[] args2) {
	}

}
